﻿using SuperJoe.Framework.Core;
using SuperJoe.Framework.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WWX.Admin.IDAL;
using WWX.Dto.Search;
using WWX.Model;

namespace WWX.Admin.BLL
{
    public class ProductBLL : BaseBLL<IProductDAL>
    {
        public Product Get(string id)
        {
            return this.dal.Get(id);
        }

        public void Add(Product product)
        {
            CheckExisted(product.No);
            product.CreatedBy = SuperJoeContext.Current.UserId;
            product.CreatedTime = DateTime.Now;
            this.dal.Add(product);
        }

        public void Delete(string id)
        {
            this.dal.Delete(id);
        }

        public void Update(Product product)
        {
            CheckExisted(product.No);

            product.UpdatedBy = SuperJoeContext.Current.UserId;
            product.UpdatedTime = DateTime.Now;
            this.dal.Update(product);
        }

        public void Activate(string id, bool isActived)
        {
            var product = this.Get(id);
            product.IsActive = isActived;
            product.UpdatedBy = SuperJoeContext.Current.UserId;
            product.UpdatedTime = DateTime.Now;
            this.dal.Update(product);
        }

        public List<Product> GetPageList(ProductAdminSearch search, int pageNo, int pageSize, out int total)
        {
            total = 0;
            return this.dal.GetPageList(search, pageNo, pageSize, out total);
        }

        private void CheckExisted(string no)
        {
            var existedProduct = this.dal.GetByNo(no);
            if (existedProduct != null)
            {
                ThrowHelper.ThrowFriendlyException("编号不能重复");
            }

        }
    }
}
